<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/3
 * Time: 14:04
 */

namespace app\api\controller;


class Search extends Base
{

    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
    }

    public function index(){
        $shu=[];
        $search= input('search');
        if (!empty($search)){

            $data=[];
            $city_map['name']=['like','%'.$search.'%'];
            $city= db('city')
                ->where($city_map)
                ->where('deleted',0)
                ->field('name,id as city_id')
                ->limit(20)
                ->select();
                if (!empty($city)){
                    foreach ($city as $k=>$v) {

                        $v['id']='city_id';
                        $data[]=$v;
                    }
                }
            $poi_map['name']=['like','%'.$search.'%'];
            $poi= db('poi')
                ->where('deleted',0)
                ->where($poi_map)
                ->field('name,id as poi_id')
                ->limit(20)
                ->select();
            if (!empty($poi)) {
                foreach ($poi as $k => $v) {
                   $v['id']='poi_id';
                    $data[]=$v;
                }




    }
            $goods_map['goods_name']=['like','%'.$search.'%'];
            $goods_map['goods_status']=1;
            $goods_map['show']=1;
            $goods= db('goods')
                ->where($goods_map)
//                ->where('goods_status',1)
                ->field('goods_name as name ,goods_id')
                ->limit(20)
                ->select();
            if (!empty($goods)) {


                        foreach ($goods as $k => $v) {
//
                                $v['id']='goods_id';
                                $data[]=$v;

                         }
            }
            $shu = [
                'code' => 0,
                'msg' => '查询成功',
                'data' => $data
            ];

            return json_encode($shu);
        }else{
            $shu = [
                'code' => 101,
                'msg' => '请输入关键字',
            ];
            return json_encode($shu);
        }







    }


    public function popular(){
$data=[];
        $shu=[];
       $city= db('city')->where('popular',1) ->field('name,id as city_id')->select();
        if (!empty($city)){
            $data['city']=$city;

        }
        $shu = [
            'code' => 0,
            'msg' => '查询成功',
            'data' => $data
        ];

        return json_encode($shu);

    }






}